package com.haofengsoft.scce.ui.mine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.platform.utils.EquipmentUtil;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.mine.ConsultationCollectModel;
import com.haofengsoft.scce.adapter.mine.ConsultationCollectionAdapter;
import com.haofengsoft.scce.adapter.mine.ConsultationCollectionInterface;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.controller.mine.ConsultationCollectController;
import com.haofengsoft.scce.db.bean.UserInfo;
import com.haofengsoft.scce.db.cache.UserCache;

public class ConsultationCollectActivity extends Activity {

	private PullToRefreshListView collectPtlv;
	private ConsultationCollectionAdapter consultationCollectionAdapter;

	private UserInfo userInfo;
	private int pageIndex = 0;

	private RelativeLayout editOperRl;
	private int editCollectSum = 0;
	private TextView editDelTv;
	private CheckBox editCb;
	private CheckBox editAllCheckCb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mine_consultation_collect);
		userInfo = UserCache.getInstance().getUser(
				ConsultationCollectActivity.this);
		ImageView backIv = (ImageView) findViewById(R.id.consultation_collect_back_iv);
		backIv.setOnClickListener(new BackClickListener());
		collectPtlv = (PullToRefreshListView) findViewById(R.id.consultation_collect_ptrl);
		editCb = (CheckBox) findViewById(R.id.consultation_collect_edit_cb);
		editCb.setOnCheckedChangeListener(new EditCheckChangeLister());
		editOperRl = (RelativeLayout) findViewById(R.id.consultation_collect_edit_rl);
		editAllCheckCb = (CheckBox) findViewById(R.id.consultation_collect_allsel_cb);
		editAllCheckCb.setOnCheckedChangeListener(new AllCheckClickListener());
		editDelTv = (TextView) findViewById(R.id.consultation_collect_del_tv);
		editDelTv.setOnClickListener(new DelCollectionClickListener());
		initCollectionVolleyData();
	}

	class BackClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			finish();
		}

	}

	private void initCollectionVolleyData() {
		Map<String, Object> mapParam = new HashMap<String, Object>();
		mapParam.put("PageIndex", pageIndex);
		mapParam.put("PageSize", 10);
		mapParam.put("ColEmpID",
				EquipmentUtil.deviceId(ConsultationCollectActivity.this));
		VolleyManage.getInstance().methodPost(ConsultationCollectActivity.this,
				null, ConstantConfig.CONSULTATION_COLLECTION_URL, mapParam,
				new CollectionListVolleyReal());
	}

	class CollectionListVolleyReal implements VolleyInterface {

		@Override
		public void gainData(VolleyBean bean) {
			if (bean.isSuccess()) {
				List<ConsultationCollectModel> modelList = ConsultationCollectController
						.getInstance(ConsultationCollectActivity.this)
						.gainConsultationCollectModelList(bean.getContent());
				refreshListData(modelList);
			} else {
				Toast.makeText(ConsultationCollectActivity.this,
						bean.getMessage(), Toast.LENGTH_SHORT).show();
			}
		}

	}

	private void refreshListData(List<ConsultationCollectModel> modelList) {
		pageIndex += 1;
		if (null == consultationCollectionAdapter) {
			consultationCollectionAdapter = new ConsultationCollectionAdapter(
					ConsultationCollectActivity.this, modelList,
					new ConsultationCollectionReal());
			collectPtlv.setAdapter(consultationCollectionAdapter);
		} else {
			consultationCollectionAdapter.addConsultationCollectList(modelList);
			consultationCollectionAdapter.notifyDataSetChanged();
		}

	}

	class EditCheckChangeLister implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton checkedButton,
				boolean isChecked) {
			consultationCollectionAdapter.configCheckMap(false);
			if (isChecked) {
				editCb.setText("完成");
				consultationCollectionAdapter.setEdit(true);
				consultationCollectionAdapter.notifyDataSetChanged();
				editOperRl.setVisibility(View.VISIBLE);
			} else {
				editCb.setText("编辑");
				consultationCollectionAdapter.setEdit(false);
				consultationCollectionAdapter.notifyDataSetChanged();
				editOperRl.setVisibility(View.GONE);
			}
		}
	}

	class ConsultationCollectionReal implements ConsultationCollectionInterface {

		@Override
		public void checkChange() {
			refreshDelTv();
		}

	}

	class AllCheckClickListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton checkedButton,
				boolean isChecked) {
			consultationCollectionAdapter.configCheckMap(isChecked);
			consultationCollectionAdapter.notifyDataSetChanged();
			if (isChecked) {
				editAllCheckCb.setText("全不选");
				editAllCheckCb.setTextColor(getResources().getColor(
						R.color.common_default_tv_pre));
			} else {
				editAllCheckCb.setText("全选");
				editAllCheckCb.setTextColor(getResources().getColor(
						R.color.common_default_tv_nor));
			}
			refreshDelTv();
		}

	}

	class DelCollectionClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			consultationCollectionAdapter.delItem();
			consultationCollectionAdapter.notifyDataSetChanged();
			refreshDelTv();
		}
	}

	private void refreshDelTv() {
		editCollectSum = consultationCollectionAdapter.gainCheckedSum();
		if (editCollectSum == 0) {
			editDelTv.setCompoundDrawablesWithIntrinsicBounds(getResources()
					.getDrawable(R.drawable.common_delete_icon_nor), null,
					null, null);
			editDelTv.setText("删除");
			editDelTv.setTextColor(getResources().getColor(
					R.color.common_default_tv_nor));
		} else {
			editDelTv.setTextColor(getResources().getColor(
					R.color.common_default_tv_pre));
			editDelTv.setCompoundDrawablesWithIntrinsicBounds(getResources()
					.getDrawable(R.drawable.common_delete_icon_pre), null,
					null, null);
			editDelTv.setText("删除（" + editCollectSum + "）");
		}
	}

}
